package net.mikoo.seals.api.model.devices;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import net.mikoo.seals.api.model.BaseResponse;
import net.mikoo.seals.api.model.ResultType;
import net.mikoo.seals.common.enums.DevicesStatusEnum;
import net.mikoo.seals.common.enums.UsedStatusEnum;
import net.mikoo.seals.model.Devices;
import net.mikoo.seals.model.DevicesAct;
import net.mikoo.seals.model.Seal;

public class GetDetailResponse extends BaseResponse {

	private static final long serialVersionUID = -301211494177843723L;

	private DevicesDetailInfo devicesInfo;

	public DevicesDetailInfo getDevicesInfo() {
		return devicesInfo;
	}

	public void setDevicesInfo(DevicesDetailInfo devicesInfo) {
		this.devicesInfo = devicesInfo;
	}
	
	public void setData(Devices devices, List<DevicesAct> actList) {
		if(devices == null) {
			return ;
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		
		DevicesDetailInfo devicesInfo = new DevicesDetailInfo();
		devicesInfo.setDevicesCode(devices.getDevicesCode());
		DevicesStatusEnum status = DevicesStatusEnum.getByCode(devices.getDevicesStatus());
		if(status != null) {
			devicesInfo.setStatus(status.getCode());
			devicesInfo.setStatusName(status.getMessage());
		}
		devicesInfo.setUseTimes(devices.getUseTimes());
		devicesInfo.setApplyTimes(devices.getApplyTimes());
		if(devices.getExpireDate() != null) {
			devicesInfo.setExpireDate(sdf.format(devices.getExpireDate()));
		}
		
		devicesInfo.setModel(devices.getModel());
		devicesInfo.setSign(devices.getSign());
		devicesInfo.setVersion(devices.getDevicesVerson());
		if(devices.getSeal() != null) {
			Seal seal = devices.getSeal();
			devicesInfo.setSealId(seal.getSealId());
			if(StringUtils.isNotBlank(seal.getSealName())) {
				devicesInfo.setSealName(seal.getSealName());
				devicesInfo.setIconText(seal.getSealName().substring(0, seal.getSealName().length() < 2 ? 1 : 2));
			}
			
			long currentTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).toInstant(ZoneOffset.of("+8")).toEpochMilli();
			if(seal.getInterimExpiredDate() != null && seal.getInterimExpiredDate().getTime() >= currentTime) {
				devicesInfo.setInterimPartyId(seal.getInterimPartyId());
				devicesInfo.setInterimPartyName(seal.getInterimPartyName());
			}
			
			devicesInfo.setSealStatus(seal.getSealStatus());
			
			devicesInfo.setPartyId(seal.getPartyId());
			devicesInfo.setPartyName(seal.getCustosName());
			devicesInfo.setIsForcePhoto(seal.getIsForcedPhoto());
		}
		
		devicesInfo.setSurplusTestNum(getSurplusTestNum(actList));
		devicesInfo.setWaitUploadActId(getWaitUploadActId(actList));
		
		this.setDevicesInfo(devicesInfo);
		this.setResult(ResultType.SUCCESS.getValue());
	}
	
	private static Integer getSurplusTestNum(List<DevicesAct> actList) {
		Integer result = 0;
		if(actList == null || actList.isEmpty()) {
			return result;
		}
		
		for(DevicesAct devicesAct : actList) {
			if(devicesAct != null) {
				result += (devicesAct.getTotalTimes() - devicesAct.getUseTimes());
			}
		}
		return result;
	}
	
	private static Long getWaitUploadActId(List<DevicesAct> actList) {
		if(actList == null || actList.size() == 0) {
			return null;
		}
		
		for(DevicesAct act : actList) {
			if(act != null && UsedStatusEnum.WAIT_UPLOAD_CERT.getCode().equals(act.getUsedStatus())) {
				return act.getDevicesActId();
			}
		}
		return null;
	}
	
}
